<?php

if (!class_exists("stthemefunctions")) {

    class stthemefunctions {

        public $option = '';

        public function __construct($config) {
            $this->config = $config;
            $this->config ['themeDir'] = TEMPLATEPATH . '/admin-functions/';
            $this->config['includePath'] = TEMPLATEPATH . '/stInclude/';
            $this->option = get_option($this->config['theme_name'] . '_' . $this->config['option_key'] . '_settings');
        }

        public function loadmodel($modelName) {
            $className = $modelName . "_model";
            $fileName = $this->config ['includePath'] . "models/{$modelName}.php";
            require_once $fileName;
            $obj = new $className($this->config);
            return $obj;
        }

        public static function getClientIp() {
            $ip;
            if (getenv("HTTP_CLIENT_IP"))
                $ip = getenv("HTTP_CLIENT_IP");
            else if (getenv("HTTP_X_FORWARDED_FOR"))
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            else if (getenv("REMOTE_ADDR"))
                $ip = getenv("REMOTE_ADDR");
            else
                $ip = "UNKNOWN";
            return $ip;
        }

        public static function getBrowerType() {
            // Declare known browsers to look for
            $known = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape',
                'konqueror', 'gecko');

            // Clean up agent and build regex that matches phrases for known browsers
            // (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor
            // version numbers.  E.g. "2.0.0.6" is parsed as simply "2.0"
            $agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
            $pattern = '#(?<browser>' . join('|', $known) .
                    ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';

            // Find all phrases (or return empty array if none found)
            if (!preg_match_all($pattern, $agent, $matches))
                return array();

            // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
            // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
            // in the UA).  That's usually the most correct.
            $i = count($matches['browser']) - 1;
            return array('browser' => $matches['browser'][$i], 'version' => $matches['version'][$i]);
        }

        
        public function sendMessage($message, $action = '') {
            if (!empty($action)) {
                return "<script type='text/javascript'>"
                . 'jQuery(document).ready(function(){'
                . "sttheme.loadPage('{$action}', '', '{$message}');"
                . '});'
                . "</script>"
                ;
            } else {
                return "<script type='text/javascript'>"
                . 'jQuery(document).ready(function(){'
                . "sttheme.sendMessage('$message');"
                . '});'
                . "</script>"
                ;
            }
        }

        public function Redirect($location) {
            return "<script type='text/javascript'>"
            . "window.location = '$location';"
            . "</script>"
            ;
        }

        public function pagination($start = NULL, $limit = NULL, $total = NULL, $cur_page = 1, $action = '') {
            if ($total > $limit) {
                $start = $start ? $start : 0;
                $limit = $limit ? $limit : $this->config['per_page'];
                $total = $total ? $total : 0;

                $total_page = ceil($total / $limit);
                $page_num = $this->config['page_num'];
                $cur_step = ceil($cur_page / $page_num);

                $max_page = ($cur_step * $page_num < $total_page) ? $cur_step * $page_num : $total_page;

                $min_page = ($cur_step - 1) * $page_num;
                $min_page = $min_page == 0 ? 1 : $min_page;

                if ($cur_page == $max_page) {
                    $min_page += 1;
                    $max_page = ($max_page + 1 < $total_page) ? ($max_page + 1) : $total_page;
                }

                $html = '<div id="st_pagination">';
                $html .= '<ul>';
                if ($cur_page > 1) {
                    $html .= "<li><a href='javascript:void(0)' onclick='{$this->config['theme_name']}.paging(0, \"$action\")' >&laquo;</a></li>";
                    $html .= "<li><a href='javascript:void(0)' onclick='{$this->config['theme_name']}.paging(" . ($cur_page - 1) . ", \"$action\")' >&lsaquo;</a></li>";
                }
                for ($i = $min_page; $i <= $max_page; $i++) {
                    if ($i == 0 || $i == $cur_page)
                        $current = 'class="current"';
                    else
                        $current = '';
                    $html .= "<li $current><a href='javascript:void(0)' onclick='{$this->config['theme_name']}.paging($i, \"$action\")' >$i</a></li>";
                }
                if ($cur_page < $total_page) {
                    $html .= "<li><a href='javascript:void(0)' onclick='{$this->config['theme_name']}.paging(" . ($cur_page + 1) . ", \"$action\")' >&rsaquo;</a></li>";
                    $html .= "<li><a href='javascript:void(0)' onclick='{$this->config['theme_name']}.paging($total_page, \"$action\")' >&raquo;</a></li>";
                }

                $html .= '</ul>';
                $html .= '</div>';
                return $html;
            } else {
                return;
            }
        }

        public function disguise_curl($url) {
            $curl = curl_init();

            // Setup headers - I used the same headers from Firefox version 2.0.0.6
            // below was split up because php.net said the line was too long. :/
            $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
            $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            $header[] = "Cache-Control: max-age=0";
            $header[] = "Connection: keep-alive";
            $header[] = "Keep-Alive: 300";
            $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
            $header[] = "Accept-Language: en-us,en;q=0.5";
            $header[] = "Pragma: "; // browsers keep this blank.


            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
            curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
            curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
            curl_setopt($curl, CURLOPT_AUTOREFERER, true);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_TIMEOUT, 10);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

            $html = curl_exec($curl); // execute the curl command
            curl_close($curl); // close the connection

            return $html; // and finally, return $html
        }

    }

}
?>